package com.edward.volleydemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.edward.volleydemo.bean.Weather;
import com.edward.volleydemo.bean.WeatherInfo;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;

public class DefinedRequestActivity extends AppCompatActivity {
    private TextView tvResult;

    private RequestQueue mRequestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_request);

        tvResult = (TextView) findViewById(R.id.tv_result);

        //1.创建RequestQueue
        mRequestQueue = Volley.newRequestQueue(this);
    }

    public void loadData(View view) {
        GsonRequest<Weather> gsonRequest = new GsonRequest<Weather>(
                Request.Method.GET,
                "http://www.weather.com.cn/data/sk/101010100.html",
                Weather.class,
                new Response.Listener<Weather>() {
                    @Override
                    public void onResponse(Weather weather) {
                        StringBuffer sb = new StringBuffer();
                        WeatherInfo weatherInfo = weather.getWeatherinfo();
                        sb.append("城市:" + weatherInfo.getCity() + "\n");
                        sb.append("温度:" + weatherInfo.getTemp() + "\n");
                        sb.append("时间:" + weatherInfo.getTime() + "\n");

                        tvResult.setText(sb.toString());
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        tvResult.setText(volleyError.getMessage());
                    }
                }
        );

        //3.添加Request对象到RequestQueue
        mRequestQueue.add(gsonRequest);
    }

    /**
     * xml 请求
     *
     * @param view
     */
    public void loadData1(View view) {
        //2.创建请求对象
        XMLRequest xmlRequest = new XMLRequest(
                Request.Method.GET,
                "http://flash.weather.com.cn/wmaps/xml/china.xml",
                new Response.Listener<XmlPullParser>() {
                    @Override
                    public void onResponse(XmlPullParser xmlPullParser) {
                        //解析xml
                        try {
                            int eventType = xmlPullParser.getEventType();
                            StringBuilder sb = new StringBuilder();
                            while (eventType != XmlPullParser.END_DOCUMENT) {
                                switch (eventType) {
                                    case XmlPullParser.START_TAG:
                                        String itemName = xmlPullParser.getName();
                                        if ("city".equals(itemName)) {
                                            sb.append("城市:" + xmlPullParser.getAttributeValue(2));
                                            sb.append("   天气状态:" + xmlPullParser.getAttributeValue(5) + "\n");
                                        }
                                        break;
                                }

                                eventType = xmlPullParser.next();
                            }

                            tvResult.setText(sb.toString());
                        } catch (XmlPullParserException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        tvResult.setText(volleyError.getMessage());
                    }
                }
        );

        //3.添加Request对象到RequestQueue
        mRequestQueue.add(xmlRequest);
    }
}
